﻿using Common.Contract;
using Common.Contract.Customer;
using Common.Contract.Customer.Request;
using Common.Contract.SaleOut;
using Common.Contract.SaleOut.Request;
using Common.Contract.Warranty.Request;
using Common.Service;
using Common.Service.Command;
using Common.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Warranty.Configuration;
using Warranty.Model.Interface;
using Warranty.Model.Model;

namespace Warranty.ServiceCommand
{
    public class UpdateWarrantyHistoryServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateWarrantyHistoryRequest, ResponseRecord>
    {
        public UpdateWarrantyHistoryServiceCommand(IProviderFactory provider) : base(provider) { }

        protected override void Validate(UpdateWarrantyHistoryRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var record = Request.Record;
            var obj = new WarrantyHistory()
            {
                Cmdcode = record.Cmdcode,
                CreateDate = record.CreateDate,
                EdiFile = record.EdiFile,
                EdiHead = record.EdiHead,
                EndUserCell = record.EndUserCell,
                Model = record.Model,
                MoMsgbody = record.Messagebody,
                Moseq = record.Moseq,
                ReceiveDate = record.ReceiveDate,
                RespMsg = record.ResponseMessage,
                RespType = record.ResponseType,
                SerialNo = record.SerialNumber,
                ShopCell = record.ShopCell,
                ShopCode = record.ShopCode,
                SmsYn = record.SmsYn.HasValue ? record.SmsYn.Value.ToBooleanString() : null,
                SoTransferFlag = record.TransferFlag.HasValue ? record.TransferFlag.Value.ToBooleanString() : null,
                SuccessFlag = record.SuccessFlag.HasValue ? record.SuccessFlag.Value.ToBooleanString() : null,
            };
            return Providers.WarrantyHistory.UpdateWarrantyHistory(obj);
        }
    }
}
